package c5;

import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

public class PuttingIntoPractice{
    public static void main(String ...args){    
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
		
		List<Transaction> transactions = Arrays.asList(
            new Transaction(brian, 2011, 300), 
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),	
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
        );	

		//1.找出2011年发生的所有交易，并按交易额排序(从低到高)
        List<Transaction> list1=transactions.stream()
                .filter(t->t.getYear()==2011)
                .sorted(comparing(Transaction::getValue))
                .collect(toList());
        System.out.println(list1);

        //2.交易员都在哪些不同的城市工作过?
        List<String> list2=transactions.stream()
                .map(t->t.getTrader().getCity())
                .distinct()
                .collect(toList());
        System.out.println(list2);

        //3.查找所有来自于剑桥的交易员，并按姓名排序。
        List<Trader> list3=transactions.stream()
                .map(Transaction::getTrader)
                .distinct()
                .filter(trader -> "Cambridge".equals(trader.getCity()))
                .sorted(comparing(Trader::getName))
                .collect(toList());
        System.out.println(list3);

        //4.返回所有交易员的姓名字符串，按字母顺序排序。
        String allNames=transactions.stream()
                .map(t->t.getTrader().getName())
                .distinct()
                .sorted(String::compareTo)
                .reduce("",(s1,s2)->s1+s2);
        System.out.println(allNames);

        //5.有没有交易员是在米兰工作的?
        boolean inMilan=transactions.stream()
                .map(t->t.getTrader().getCity())
                .anyMatch("Milan"::equals);
        System.out.println(inMilan);

        //6.打印生活在剑桥的交易员的所有交易额。
        transactions.stream()
                .filter(t->t.getTrader().getCity().equals("Cambridge"))
                .mapToInt(Transaction::getValue)
                .forEach(System.out::println);

        //7.所有交易中，最高的交易额是多少
        OptionalInt max=transactions.stream()
                .mapToInt(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println("交易额最大："+(max.isPresent()?max.getAsInt():"null"));

        //8.找出交易额最小的交易
        Transaction minTrans=transactions.stream()
                .reduce((t1,t2)->t1.getValue()<t2.getValue()?t1:t2)
                .orElse(null);
        System.out.println("最小交易额的交易："+minTrans);

        //第二种
        Transaction minTrans2=transactions.stream()
                .min(comparing(Transaction::getValue))
                .orElse(null);
        System.out.println("最小交易额的交易："+minTrans2);




       /* // Query 1: Find all transactions from year 2011 and sort them by value (small to high).
        List<Transaction> tr2011 = transactions.stream()
                                               .filter(transaction -> transaction.getYear() == 2011)
                                               .sorted(comparing(Transaction::getValue))
                                               .collect(toList());
        System.out.println(tr2011);
        
        // Query 2: What are all the unique cities where the traders work?
        List<String> cities = 
            transactions.stream()
                        .map(transaction -> transaction.getTrader().getCity())
                        .distinct()
                        .collect(toList());
        System.out.println(cities);

        // Query 3: Find all traders from Cambridge and sort them by name.
        
        List<Trader> traders = 
            transactions.stream()
                        .map(Transaction::getTrader)
                        .filter(trader -> trader.getCity().equals("Cambridge"))
                        .distinct()
                        .sorted(comparing(Trader::getName))
                        .collect(toList());
        System.out.println(traders);
        
        
        // Query 4: Return a string of all traders’ names sorted alphabetically.
        
        String traderStr = 
            transactions.stream()
                        .map(transaction -> transaction.getTrader().getName())
                        .distinct()
                        .sorted()
                        .reduce("", (n1, n2) -> n1 + n2);
        System.out.println(traderStr);
        
        // Query 5: Are there any trader based in Milan?
        
        boolean milanBased =
            transactions.stream()
                        .anyMatch(transaction -> transaction.getTrader()
                                                            .getCity()
                                                            .equals("Milan")
                                 );
        System.out.println(milanBased);
        
        
        // Query 6: Update all transactions so that the traders from Milan are set to Cambridge.
        transactions.stream()
                    .map(Transaction::getTrader)
                    .filter(trader -> trader.getCity().equals("Milan"))
                    .forEach(trader -> trader.setCity("Cambridge"));
        System.out.println(transactions);
        
        
        // Query 7: What's the highest value in all the transactions?
        int highestValue = 
            transactions.stream()
                        .map(Transaction::getValue)
                        .reduce(0, Integer::max);
        System.out.println(highestValue);    */
    }
}